package com.ys.controller.api.finance;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.ys.config.MenuId;
import com.ys.entity.finance.TcwRunningBack;
import com.ys.entity.finance.TcwRunningBackDetail;
import com.ys.service.finance.TcwRunningBackService;
import com.ys.service.flow.YspTaskService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tcwRunningBack")
public class ApiInterTcwRunningBackController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private TcwRunningBackService tcwRunningBackService;
	@Autowired
	private YspTaskService yspTaskService;
	
	@Autowired
	private CommonApiService<TcwRunningBackDetail> commonApiServiceDetail;

	@RequiresPermissions("tcwRunningBack:view")
	@RequestMapping("/searchDetail")
    @ResponseBody
	public LayuiTableVoJA searchDetail(@ModelAttribute TcwRunningBackDetail cond){
		
		cond.addLeftJoin("TCW_RunningBack B ON B.Bid = A.Pbid")
			.addField("B.EffectStock effectStock")
			.addField("B.Id pid");
		
		LayuiTableVoJA vo = commonApiServiceDetail.searchLay(cond);
		return vo;
	}
	
	/**
	 * 查找关联流水
	 * @throws Exception 
	 */
	@PostMapping("/checkForAdd")
    @ResponseBody
	public R checkForAdd(HttpServletRequest request, @RequestParam Map<String, Object> params,
			String ids) {
	
		return tcwRunningBackService.checkForAdd(ids);
	}
	
	/**
	 * 新增
	 * @throws Exception 
	 */
	@RequiresPermissions("tcwRunningBack:add")
	@PostMapping("/add")
    @ResponseBody
	public R add(HttpServletRequest request,
			String bid,
			String urlFile,
			String subData) throws Exception{
		
		return tcwRunningBackService.add(bid, urlFile, subData, MenuId.TCW_RunningBack);
	}
	
	/**
	 * 流转通过
	 */
	@RequiresPermissions("tcwRunningBack:flow")
	@PostMapping("/flowPass")
    @ResponseBody
	public R flowPass(HttpServletRequest request, Long id){
		try {
			return tcwRunningBackService.flowPass(id, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(JSON.toJSONString(e));;
			return R.error("服务器正在开小差，请联系客服");
		}
	}
	
	/**
	 * 流转驳回
	 */
	@RequiresPermissions("tcwRunningBack:flow")
	@PostMapping("/flowRefuse")
    @ResponseBody
	public R flowRefuse(HttpServletRequest request, Long id, String reason){
		
		TcwRunningBack entity = new TcwRunningBack().setId(id).queryFirst();
		String bid = entity.getBid();
		try {
			return yspTaskService.flowRefuse(bid, reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}


	/**
	 * 流转驳回
	 */
	@RequiresPermissions("tcwRunningBack:flow")
	@PostMapping("/flowInit")
	@ResponseBody
	public R flowInit(HttpServletRequest request, Long id, String reason){

		TcwRunningBack entity = new TcwRunningBack().setId(id).queryFirst();
		String bid = entity.getBid();
		try {
			return yspTaskService.flowInit(bid, reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}
	
	/**
	 * 删除
	 * @throws Exception 
	 */
	@RequiresPermissions("tcwRunningBack:delete")
	@PostMapping("/delete")
    @ResponseBody
	public R delete(Long id){
		
		try {
			return tcwRunningBackService.deleteTrue(id);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("操作失败，请联系客服");
		}
	}
}
